package com.tedu.cooshark.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tedu.cooshark.dao.ProductMapper;
import com.tedu.cooshark.entity.Category;
import com.tedu.cooshark.entity.Product;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 2022/3/26  10:40
 */
@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    public List<Product> selectAll(){
        return productMapper.selectAll();
    }
    public PageInfo<Product> selectAllPage(int page, int offset){
        PageHelper.startPage(page,offset);
        List<Product> products = productMapper.selectPage();
        //获得分页相关的参数
        PageInfo<Product> Page = new PageInfo<>(products);
        System.out.println("当前页;"+Page.getPageNum());
        System.out.println("每页显示条数:"+Page.getPageSize());
        System.out.println("总条数:"+Page.getTotal());
        System.out.println("总页数;"+Page.getPages());
       return Page;
    }

    public int insertProduct(Product product){
        product.setCreateTime(new Date());
       return productMapper.insertProduct(product);
    }
    public int deleteByid(Product product){
        return productMapper.deleteByid(product);
    }
    public int deleteProductByids(List<Product> products){
        int n=0;
        for (Product product : products) {
            productMapper.deleteByid(product);
            n++;
        }
        return n;
    }
    public int updateProduct(@Param("product") Product product){
       return productMapper.updateProduct(product);
    }
}
